﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class ReplyDAL
    {
        public static int Save(ReplyInfo info)
        {
            string sql = string.Format("insert into [reply]([SID],[UserID],[Lat],[Lng],[Address],[Context],[Flag],[State],[CreateDate]) values({0},{1},{2},{3},'{4}','{5}','{6}',{7},'{8}');select @@identity;", info.SID, info.UserID, info.Lat, info.Lng, info.Address, info.Context, info.Flag, info.State, info.CreateDate);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }
        public static List<ReplyInfo> FindListBySID(int sid,string flag)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select [reply].*,[user].RealName as RealName,[user].HeadImg as HeadImg from [reply] left join [user] on [reply].UserID=[user].ID where SID={0} and Flag='{1}' order by ID desc",sid,flag));
            List<ReplyInfo> list = new List<ReplyInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(All_Data(row));
            }
            return list;
        }
        public static List<ReplyInfo> FindListBySIDs(string sids, string flag)
        {
            
            List<ReplyInfo> list = new List<ReplyInfo>();
            if (sids != "")
            {
                DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [reply] where SID in ({0}) and Flag='{1}' order by ID desc", sids, flag));
                foreach (DataRow row in dt.Rows)
                {
                    list.Add(Single_Data(row));
                }
            }
            return list;
        }
        private static ReplyInfo Single_Data(DataRow dr)
        {
            ReplyInfo info = new ReplyInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Lat = DataConvert.ToFloat(dr["Lat"]);
                info.Lng = DataConvert.ToFloat(dr["Lng"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.Flag = DataConvert.ToString(dr["Flag"]);
                info.State = DataConvert.ToInt(dr["State"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static ReplyInfo All_Data(DataRow dr)
        {
            ReplyInfo info = new ReplyInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Lat = DataConvert.ToFloat(dr["Lat"]);
                info.Lng = DataConvert.ToFloat(dr["Lng"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.Flag = DataConvert.ToString(dr["Flag"]);
                info.State = DataConvert.ToInt(dr["State"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.HeadImg = DataConvert.ToString(dr["HeadImg"]);
                info.HeadImg = Commons.domain + (info.HeadImg == "" ? "/system/img/head.png" : info.HeadImg);

            }
            return info;
        }
    }
}